#include <stdio.h>

#include "SDL/SDL_log.h"
#include "SDL/SDL.h"
#include "rect.h"

#define WIDTH 800
#define HEIGHT 400

void draw(SDL_Window *window){
    SDL_Surface *screen = SDL_GetWindowSurface(window);
    SDL_Rect bg = {0, 0, WIDTH, HEIGHT};
    SDL_FillRect(screen, &bg, 0xffffffff);

    if(posX > WIDTH){
       posX = 0;
    }

    createRect(screen);
    posX++;
    
    SDL_UpdateWindowSurface(window);
    SDL_Delay(10);
}

void event_loop(SDL_Window *window){
 //必须有事件处理，否则窗体出不来，
    while (1)
    {

       draw(window);

       SDL_Event event;
       while(SDL_PollEvent(&event)){
            printf("event is %#x\n", event.type); // test code
            if (SDL_QUIT == event.type) {
                return;
            }
       }
    }
}

int showWindow(){
    if(SDL_Init(SDL_INIT_VIDEO)){
       SDL_Log("init error : %s", SDL_GetError());
       return 1;
    }
    SDL_Window *window = SDL_CreateWindow("title", 
                            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                            WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
    if(window == NULL){
       SDL_Log("create window error %s", SDL_GetError());
       return 2;
    }
    event_loop(window);
    SDL_DestroyWindow(window);
}

int main()
{
    showWindow();
    return 0;
}